package util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Reader {
	public static Set<String> wordList = new HashSet<String>();//保存词表
	public static Map<Integer, List<String>> texts = new HashMap<Integer, List<String>>();//保存各个文本的词
	
	@SuppressWarnings("resource")
	public static Set<String> readfile(){
		try {
			for (int i = 1; i < 11; i++) {
				File file = new File("file/"+ i +".txt");
				List<String> tempList = new ArrayList<>();//用于保存一个文本的所有词
				Scanner reader = new Scanner(file);
				
				while (reader.hasNextLine()) {
					
					String readline = (String) reader.nextLine();
					if(readline.equals("")){
						continue;
					}
					String[] strArr = readline.split("\\s+");
					
					//过滤标点符号和制表符并添加到总词表中（不能重复），将文本词库存入临时List（完全保存文本中的所有词，可重复）
					for (String str : strArr) {
					//if(!str.contains("/w") && !str.contains("/x")){
							//str=str.replace(str.substring(str.length()-2, str.length()), "");
							wordList.add(str);
							tempList.add(str);
					//	}
					}
					//System.out.println(readline);
				}
				
				//将文本序号及与其对应的词存入Map
				texts.put(i, tempList);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return wordList;
		
	}
	
	public static void main(String[] args) {
		readfile();
		for (String word : wordList) {
			System.out.println(word);
		}

		for (Map.Entry<Integer, List<String>> entry : texts.entrySet()){
			List<String> tempList = entry.getValue();
			System.out.println(entry.getKey()+".txt\n-------------------");
			for (String word : tempList) {
				System.out.println(word);
			}
		}
	}
}
